package com.bindatax.biz.etl;

import com.bindatax.biz.etl.create.CreateJavaCode;
import com.bindatax.core.custom.GlobalConstant;

import java.io.File;
import java.util.*;


public class BinDataxBizApp {


    public final static String MODEL = "model";

    private final static Map<String,String> ARGS_MAP = new HashMap<>();

    public static void main(String[] args) {
        checkOut();
        buildArgs(args);
        switch (ARGS_MAP.get(MODEL)){
            case "createJavaCode":
                CreateJavaCode.start(ARGS_MAP.get(GlobalConstant.TASK_CONFIG_DIR));//创建java文本
                break;
            case "startSingleTask":
                startTask( ARGS_MAP.get(GlobalConstant.TASK_CONFIG_DIR) );//执行单个任务
                break;
            default:
                System.out.println("请配置model选项！");
        }
    }
    public static void buildArgs(String[] args){
        ARGS_MAP.putAll( System.getenv() );
        Properties properties = System.getProperties();
        Set<String> names = properties.stringPropertyNames();
        for (String name : names){
            ARGS_MAP.put( name , properties.getProperty(name) );
        }
        for (String arg : args) {
            if (arg != null) {
                int dex = arg.indexOf("=");
                if(dex > 0){
                    ARGS_MAP.put( arg.substring(0,dex) , arg.substring(dex+1) );
                }
            }
        }
    }

    public static void startTask(String configPath) {
        if (configPath == null) {
            System.out.println("程序错误！！！！无法找到配置文件目录！");
            return;
        }
        File configFile = new File(configPath);
        if (!configFile.exists()) {
            System.out.println("程序错误！！！！无法找到配置文件目录！");
            return;
        }
        configPath = configFile.getAbsolutePath();
        BinDataxBoot.start(configPath,null);
    }

    private static void checkOut(){
        System.out.println("启动多线程入库程序.....");
        Random random = new Random();
        System.out.println("正在自检中[0%]");
        for(int i = 1 ; i < 10 ; i++ ){
            System.out.println("正在自检中["+(i*10)+"%]");
            try {
                Thread.sleep( random.nextInt(200) + 100 );
            } catch (InterruptedException ignored) {}
        }
        System.out.println("正在自检中[100%]");
        System.out.println("程序自检完成。");
    }




}
